//
//  SNMYInfomationViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/11.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNMYInfomationViewController.h"
#import "Masonry.h"

@implementation SNMYInfomationCell
- (void)configWithLeftText:(NSString *)leftText rightText:(NSString *)rightText{
    self.nameLeftLabel.text = leftText;
    self.nameRightLabel.text = rightText;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addViews];
        [self setupUI];
    }
    return self;
}

- (void)addViews
{
    UILabel * nameLeftLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"组织机构代码";
        label.font = [UIFont systemFontOfSize:15];
        
        // [self addSubview:label];
        // [self.view addSubview:label];
        [self.contentView addSubview:label];
        
        
        label;
    });
    self.nameLeftLabel = nameLeftLabel;
    UILabel * nameRightLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"xxx";
        label.font = [UIFont systemFontOfSize:15];
        
        // [self addSubview:label];
        // [self.view addSubview:label];
        [self.contentView addSubview:label];
        
        
        label;
    });
    self.nameRightLabel = nameRightLabel;
}
- (void)setupUI
{
    CGSize size = [[[NSAttributedString alloc] initWithString:@"组织机构代码" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    [self.nameLeftLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(11);
        make.centerY.mas_equalTo(self.contentView.mas_centerY).offset(0);
        make.width.mas_equalTo(size.width+1);
    }];
    
    [self.nameRightLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.nameLeftLabel.mas_right).offset(19);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-11);
        make.centerY.mas_equalTo(self.contentView.mas_centerY).offset(0);
        make.top.mas_equalTo(self.contentView.mas_top).offset(19).priorityLow();
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-19).priorityLow();
    }];
}

@end


//=************************ stone ***
@interface SNMYInfomationViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;

/** keyStrings */
@property (nonatomic,strong) NSArray * keyStrings;
/** valueStrings */
@property (nonatomic,strong) NSArray * valueStrings;
@end

@implementation SNMYInfomationViewController
/** keyStrings 懒加载 */
- (NSArray *)keyStrings {
    if (_keyStrings == nil) {
        
        _keyStrings = [NSArray array];
        
        switch (kUserInfo.userType.integerValue) {
            case 1://小农
                _keyStrings = @[@"姓名",@"身份证号码",@"手机号码",@"所在地区",@"身份",@"隶属组织",@"承包土地面积",@"实际土地面积"];
                break;
            case 2://农场员工
                _keyStrings = @[@"姓名",@"身份证号码",@"手机号码",@"所在地区",@"身份",@"隶属组织",@"承包土地面积",@"实际土地面积"];//@[@[@"农场名称",@"所在地区",@"组织机构代码"],@[@"姓名",@"手机号码"]];
                break;
            case 3://散户
               _keyStrings = @[@"姓名",@"身份证号码",@"手机号码",@"所在地区",@"身份",@"承包土地面积",@"实际土地面积"];//@[@"姓名",@"身份证号码",@"手机号码",@"所在地区",@"身份"];
                break;
            case 4://决策者
                
                break;
            case 5://联社业务代表
            case 6://组织业务代表
                _keyStrings = @[@"姓名",@"手机号码"];
                break;
                
            default:
                
                break;
        }
    }
    return _keyStrings;
}
/** valueStrings 懒加载 */
- (NSArray *)valueStrings {
    if (_valueStrings == nil) {
        
        _valueStrings = [NSArray array];
        
        switch (kUserInfo.userType.integerValue) {
            case 1://小农
                _valueStrings = @[kUserDetailInfo.custName,kUserDetailInfo.idCard,kUserDetailInfo.tel,kUserDetailInfo.filteredAddress,kUserDetailInfo.usertypename,kUserDetailInfo.fullName,[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.landArea],[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.actualArea]];
                break;
            case 2://农场员工
                _valueStrings = @[kUserDetailInfo.custName,kUserDetailInfo.idCard,kUserDetailInfo.tel,kUserDetailInfo.filteredAddress,kUserDetailInfo.usertypename,kUserDetailInfo.fullName,[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.landArea],[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.actualArea]];//@[@[kUserDetailInfo.fullName,kUserDetailInfo.filteredAddress,kUserDetailInfo.fullName],@[kUserDetailInfo.custName,kUserDetailInfo.tel]];
                break;
            case 3://散户
                _valueStrings = @[kUserDetailInfo.custName,kUserDetailInfo.idCard,kUserDetailInfo.tel,kUserDetailInfo.filteredAddress,kUserDetailInfo.usertypename,[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.landArea],[NSString stringWithFormat:@"%@(公顷)",kUserDetailInfo.actualArea]];//@[kUserDetailInfo.custName,kUserDetailInfo.idCard,kUserDetailInfo.tel,kUserDetailInfo.filteredAddress,kUserDetailInfo.usertypename];
                break;
            case 4://决策者
                
                break;
            case 5://联社业务代表
                
            case 6://组织业务代表
                
                _valueStrings = @[kUserInfo.userName,kUserInfo.tel];
                break;
                
            default:
                
                break;
        }
    }
    return _valueStrings;
}
//===================================== stone ===========/
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = HexRGB(0xffffff);
//    switch (kUserInfo.userType.integerValue) {
//        case 3:
//            self.navigationItem.title = @"详细信息";
//            break;
//        default:
            self.navigationItem.title = @"我的资料";
//            break;
//    }
    
    CGRect frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self setupInit:tableView];
    
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}

#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}

- (void)setupInit:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorInset = UIEdgeInsetsZero;
    tableView.backgroundColor = HexRGB(0xf5f5f5);
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    [self setHeights:tableView];
    [self registerNib:tableView];
    [self registerClass:tableView];
}
#pragma mark - <Register>
/** registerNib */
- (void)registerNib:(UITableView *)tableView {
    //    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SNTableViewCell class])];
}
/** registerClass */
- (void)registerClass:(UITableView *)tableView {

    [tableView registerClass:[SNMYInfomationCell class] forCellReuseIdentifier:NSStringFromClass([SNMYInfomationCell class])];
}
#pragma mark - <SetHeights>
/** setHeights */
- (void)setHeights:(UITableView *)tableView {
    
    /** cell */
    tableView.estimatedRowHeight = 47;
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    tableView.sectionHeaderHeight = 10;
    tableView.sectionFooterHeight = 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
//    if (kUserInfo.userType.integerValue == 2) {
//        return self.keyStrings.count;
//    }
    return 1;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
//    if (kUserInfo.userType.integerValue == 2) {
//        return [self.keyStrings[section] count];
//    }
    return self.keyStrings.count;
}

//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (kUserInfo.userType.integerValue == 2){
//        
//        
//        SNMYInfomationCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNMYInfomationCell class])];
//        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        
//        [cell configWithLeftText:self.keyStrings[indexPath.section][indexPath.row] rightText:self.valueStrings[indexPath.section][indexPath.row]];
//        return cell;
//    }
    
    
    
    SNMYInfomationCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNMYInfomationCell class])];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell configWithLeftText:self.keyStrings[indexPath.row] rightText:self.valueStrings[indexPath.row]];
    
    return cell;
    
//    return cell;
}

#pragma mark - <UITableViewDelegate>

///** 选中一行 */
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%s", __func__);
//    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
//}
///** 取消 选中 */
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
//    NSLog(@"%s", __func__);
//}


@end
